package com.kdgame.ggtest.unitylib;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import org.json.JSONObject;

public abstract class BaseUnityActivity extends UnityPlayerActivity implements Handler.Callback {
    protected static final String TAG = "BaseUnityActivity";
    //消息标识
    private static final int MSG_INIT = 100; //初始化
    private static final int MSG_LOGIN = 101; //登录
    private static final int MSG_LOGOUT = 102; //登出
    private static final int MSG_PAY = 103; //支付
    private static final int MSG_EXIT = 104; //登出
    private static final int MSG_ROLE_INFO = 105; //角色信息
    private static final int MSG_PLAY_AD = 106; //播放广告视频
    private static final int MSG_USER_CENTER = 107; //用户中心

    //当前Context对象引用
    protected Context mContext = null;
    //Android中用于切换线程的对象
    private Handler mHandler = new Handler(this);
    //Unity与Android通信的gameObject名称
    private String gameObjectName = "";

    /**
     * Android生命周期方法
     *
     * @param bundle
     */
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        try {
            this.mContext = UnityPlayer.currentActivity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Unity Activity
     *
     * @return
     */
    public Activity getUnityActivity() {
        return UnityPlayer.currentActivity;
    }

    /**
     * 输出日志到控制台
     *
     * @param str
     */
    public void XLog(String str) {
        Log.e(TAG, str);
    }

    /**
     * Android调用Unity帮助方法
     *
     * @param str
     * @param str2
     */
    public void callUnityFunc(String str, String str2) {
        if (!TextUtils.isEmpty(this.gameObjectName)) {
            UnityPlayer.UnitySendMessage(this.gameObjectName, str, str2);
            return;
        }
        XLog("callUnityFunc error, gameObjectName" + this.gameObjectName + "funcName:" + str + " paramStr:" + str2);
        throw new NullPointerException("gameObject is null, please set gameObject first");
    }

    /**
     * 设置当前监听Android消息回调的Unity对象
     *
     * @param str
     */
    public void setUnityGameObjectName(String str) {
        this.gameObjectName = str;
    }

    /**
     * 将Unity发送的消息切换到主线程
     *
     * @param message
     * @return
     */
    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case MSG_INIT:
                XLog("init");
                onHandlerInitMessage();
                return false;
            case MSG_LOGIN:
                XLog("login");
                onHandlerLoginMessage();
                return false;
            case MSG_LOGOUT:
                Log.e(TAG, "logout");
                onHandlerLogoutMessage();
                return false;
            case MSG_PAY:
                Log.e(TAG, "pay");
                String cpOrderSN = message.obj.toString();
                onHandlerPayMessage(cpOrderSN);
                return false;
            case MSG_EXIT:
                Log.e(TAG, "exit");
                onHandlerRequestExitMessage();
                return false;
            case MSG_ROLE_INFO:
                /** CP根据情况自己实现 */
                return false;
            case MSG_PLAY_AD:
                onHandlerPlayAd();
                break;
            case MSG_USER_CENTER:
                onHandlerUserCenter();
                return false;
            default:
                return false;
        }
        return false;
    }


    /********************** 子类重写逻辑,调用SDK逻辑  **********************/
    //region 子类重写逻辑

    /**
     * 初始化
     */
    public void onHandlerInitMessage() {
    }


    /**
     * 登录
     */
    public void onHandlerLoginMessage() {
    }

    /**
     * 登出
     */
    public void onHandlerLogoutMessage() {
    }

    /**
     * 支付
     */
    public void onHandlerPayMessage(String cpOrderSN) {
    }

    /**
     * 退出
     */
    public void onHandlerRequestExitMessage() {
    }

    /**
     * 播放广告
     */
    public void onHandlerPlayAd() {
    }

    /**
     * 打开用户中心
     */
    public void onHandlerUserCenter() {
    }
    //endregion 子类重写逻辑


    /********************** Unity调用接口  **********************/
    //region Unity调用接口

    /**
     * 初始化
     */
    public void requestInit() {
        XLog("requestInit");
        this.mHandler.sendEmptyMessage(MSG_INIT);
    }

    /**
     * 登录
     */
    public void requestLogin() {
        XLog("requestLogin");
        this.mHandler.sendEmptyMessage(MSG_LOGIN);
    }

    /**
     * 登出
     */
    public void requestLogout() {
        this.mHandler.sendEmptyMessage(MSG_LOGOUT);
    }

    /**
     * 支付
     * 这里只模拟了订单号作为测试
     * 真实情况需要传递所有的参数
     *
     * @param cpOrderSN CP订单号
     */
    public void requestPay(String cpOrderSN) {
        Message obtainMessage = this.mHandler.obtainMessage(MSG_PAY);
        obtainMessage.obj = cpOrderSN;
        obtainMessage.sendToTarget();
    }

    /**
     * 退出
     */
    public void requestExit() {
        this.mHandler.sendEmptyMessage(MSG_EXIT);
    }

    /**
     * 播放广告
     */
    public void requestPlayAd() {
        this.mHandler.sendEmptyMessage(MSG_PLAY_AD);
    }

    /**
     * 打开用户中心
     */
    public void requestUserCenter() {
        this.mHandler.sendEmptyMessage(MSG_USER_CENTER);
    }
    //endregion Unity调用接口


    /********************** 回调Unity接口  **********************/
    //region  回调Unity接口
    public void sendInitSuccess() {
        callUnityFunc("MsgOnInitSuccess", "初始化成功");
    }


    public void sendInitFail() {
        callUnityFunc("MsgOnInitFailed", "初始化失败");
    }


    /**
     * 登录成功
     * @param userID sdk用户 ID
     * @param session sdk服务器通信凭证
     */
    public void sendLoginSuccess(String userID, String session) {
        //这里只是做了简单的拼接
        //CP 需要根据自己逻辑修改
        callUnityFunc("MsgOnLoginSuccess", userID+"******"+session);
    }

    public void sendLoginFail(String failMsg) {
        callUnityFunc("MsgOnLoginFailed",failMsg);
    }

    /**
     * 支付成功客户端不能以SDK返回的为准
     * 需要去服务器端验证
     */
    public void sendPaySuccess() {
        callUnityFunc("MsgOnPaySuccess", "支付成功");
    }


    /* access modifiers changed from: protected */
    public void sendPayFail() {
        callUnityFunc("MsgOnPayFailed", "支付失败");
    }

    //endregion  回调Unity接口
}
